export function hasKey(obj = {}, key) {
	if (!(obj instanceof Object) || !key || !Object.keys(obj).length) {
		return ''
	}
	if (key instanceof Array) {
		for (let i = 0; i < key.length; i++) {
			if (obj[key[i]] !== '') {
				return key[i]
			}
		}
	}
	if (typeof key === 'string') {
		return key
	}
	return ''
}

export function handleStreamData(buffer = []) {
	const decoder = new TextDecoder('utf-8');
	const text = decoder.decode(buffer);
	return JSON.parse(text)
}

export function h5_url_to_blob(url) {
	return new Promise((resolve, reject) => {
		var xhr = new XMLHttpRequest();
		xhr.open('GET', url, true);
		xhr.responseType = 'blob';
		xhr.onload = function(e) {
			if (this.status == 200) {
				var Blob = this.response;
				// console.log(myBlob)
				resolve(Blob)
				// myBlob现在是对象URL指向的blob。 
			}
		};
		xhr.send();
	})
}
export function weixinClosePage() {
	if (typeof WeixinJSBridge == "undefined") {
		if (document.addEventListener) {
			document.addEventListener('WeixinJSBridgeReady', weixin_ClosePage, false);
		} else if (document.attachEvent) {
			document.attachEvent('WeixinJSBridgeReady', weixin_ClosePage);
			document.attachEvent('onWeixinJSBridgeReady', weixin_ClosePage);
		}
	} else {
		weixin_ClosePage();
	}
}

function weixin_ClosePage() {
	WeixinJSBridge.call('closeWindow');
}